package cvi.com.reader.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import cvi.com.reader.R;
import cvi.com.reader.adapter.SelectPictureAdapter;
import cvi.com.reader.base.BaseActivity;
import cvi.com.reader.bean.PartyMemberInfoBean;
import cvi.com.reader.bean.SelectPictureBean;
import cvi.com.reader.bean.constant;
import cvi.com.reader.network.DefaultObserver;
import cvi.com.reader.network.NetWorkManager;
import cvi.com.reader.tool.UtilsOnclickListen;
import cvi.com.reader.widgets.MyGridView;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by 梅涛 on 2020/11/20.
 * 头像选择界面---单选
 */

public class SelectPictureActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.back)
    LinearLayout back;
    @BindView(R.id.tittle)
    TextView tittle;
    @BindView(R.id.sava_tv)
    TextView savaTv;
    @BindView(R.id.right_icon)
    LinearLayout rightIcon;
    @BindView(R.id.mylistview)
    MyGridView mylistview;
    private SelectPictureAdapter selectPictureAdapter;
    private List<String> selectPictureBeanList = new ArrayList<>();
    private String avatar = "";


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.sava_tv:
                if (UtilsOnclickListen.isFastClick()) {
                    saveData();
                }
                break;
        }

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_select_picture;
    }

    @Override
    protected void initView() {
        back.setOnClickListener(this);
        savaTv.setOnClickListener(this);
        tittle.setText("个人头像");
    }

    @Override
    protected void initData() {
        //党支部接口
        NetWorkManager.getInstance().getNetWorkApiService().getAvatarList().compose(this.<JSONObject>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DefaultObserver<JSONObject>(SelectPictureActivity.this) {
                    @Override
                    protected void onSuccess(JSONObject response) {
                        if (response != null) {
                            setAvatar(response);
                        }

                    }
                });
    }

    private void setAvatar(JSONObject response) {
        SelectPictureBean selectPictureBean = response.toJavaObject(SelectPictureBean.class);
        selectPictureBeanList = selectPictureBean.getData();
        selectPictureAdapter = new SelectPictureAdapter(SelectPictureActivity.this, selectPictureBeanList);
        mylistview.setAdapter(selectPictureAdapter);
        selectPictureAdapter.notifyDataSetChanged();
        mylistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                selectPictureAdapter.changeState(i);
                avatar = (String) adapterView.getAdapter().getItem(i);

            }
        });
    }

    private void saveData() {
        if (avatar.isEmpty()) {
            Toast.makeText(SelectPictureActivity.this, "未选择图片哦", Toast.LENGTH_SHORT).show();
            return;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("avatar", avatar);
        //党支部接口
        NetWorkManager.getInstance().getNetWorkApiService().updataPersonInfo(map).compose(this.<JSONObject>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DefaultObserver<JSONObject>(SelectPictureActivity.this) {
                    @Override
                    protected void onSuccess(JSONObject response) {
                        if (response != null) {
                            Toast.makeText(SelectPictureActivity.this, "修改成功！", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent();
                            intent.setAction(constant.UPDATA_PARTY_MENBER_INFO_MYRECEIVER);
                            sendBroadcast(intent);
                            finish();
                        }

                    }
                });

    }

    @Override
    protected void extrasDoSome(Bundle extras) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
